from enum import Enum
from typing import Any, List, Dict, Optional

from pydantic import BaseModel, Field, ConfigDict


# 1. 文本结构
class Text(BaseModel):
    text: str


# 2. 多媒体文件结构
class FileType(str, Enum):
    IMAGE = "image"
    VIDEO = "video"


class MediaFile(BaseModel):
    path: str
    file_name: str
    file_type: FileType


# 3. 文本规则结构
class TextRule(BaseModel):
    text_rule: List[str]
    mermaid: Optional[str] = None
    premises: Optional[List[str]] = None


# 4. 图规则结构
class GraphPatternNode(BaseModel):
    id: int
    label: str


class GraphPatternEdge(BaseModel):
    id: int
    source: int
    target: int
    label: str


class GraphPattern(BaseModel):
    nodes: List[GraphPatternNode]
    edges: List[GraphPatternEdge]


class RuleCondition(BaseModel):
    nodeID: int
    properties: Dict[str, str]


class RuleResult(BaseModel):
    nodeID: int
    properties: Dict[str, str]


class GraphRule(BaseModel):
    ruleID: int
    ruleName: str
    ruleDescription: str
    graphPattern: GraphPattern
    conditionX: List[RuleCondition]
    resultY: RuleResult
    mermaid: Optional[str] = None  # 添加 mermaid 图表示，默认为空


# 5. 本体结构
class Concept(BaseModel):
    name: str
    id: str
    description: Optional[str] = None
    is_a: Optional[str] = None  # 上位概念，可以为空


class Relation(BaseModel):
    name: str
    id: str
    description: Optional[str] = None
    domain: List[str]  # 关系的定义域，存储概念ID列表
    range: List[str]  # 关系的值域，存储概念ID列表
    is_unique: int = Field(ge=0, le=1)  # 是否唯一，0或1


class PropertyType(str, Enum):
    STRING = "string"
    NUMBER = "number"
    BOOLEAN = "boolean"
    DATE = "date"


class Property(BaseModel):
    name: str
    id: str
    description: Optional[str] = None
    property_type: PropertyType
    concept: str  # 所属概念的ID


class Ontology(BaseModel):
    concepts: List[Concept]
    properties: List[Property]
    relations: List[Relation]


# 6. 知识图谱结构
class NodeProperty(BaseModel):
    model_config = ConfigDict(arbitrary_types_allowed=True)
    name: str
    value: Any


class Node(BaseModel):
    name: str
    id: str
    properties: Optional[List[NodeProperty]] = None


class Edge(BaseModel):
    name: str
    id: str
    from_node: str  # 起始节点的ID
    to_node: str  # 终止节点的ID
    properties: Optional[List[NodeProperty]] = None

class KnowledgeGraph(BaseModel):
    nodes: List[Node]
    edges: List[Edge]
